lvs-dr配置与应用-4
=============================================
一：设计要点
    1.1：在各主机（Director,RS）均需要配置VIP；因此，要解决地址的冲突的问题，目标是让各RS上的VIP不可见，仅用接收目标地址为VIP的报文，同时可作为响应报文的源地址；
      (1) 在前端的网关接口上静态绑定；
      (2) 在各RS上使用arptables；
      (3) 在各RS上修改内核参数，来限制arp响应和通告；
        限制响应级别：arp_ignore
          0：使用本地任意接口上配置的地址进行响应；
          1：仅在请求的目标IP配置在本地主机的接入报文的接口上时，才给予响应；
          2-8: 
        限制通告级别：arp_announce
          0：默认，把本机所有接口信息向每个接口通告；
          1：尽量避免向非本网络通告；
          2：总是避免；

    1.2：设计要点：
      (1) 各主机一个接口即可，但需要在同一物理网络中；
      (2) rip的网关不能指向dip；rip和dip通常应该在同一网络，但此二者未必会与vip在同一网络；
      (3) 各rs需要先设置内核参数，再设置vip和路由；
      
      echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
      echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
      echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
      echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce

    1.3：设置接口IP:
        ifconfig  lo:0  $vip netmask 255.255.255.255 broadcast $vip 
        route  add -host  $vip  dev lo:0
        

<img src="5.png" alt="">


二：设置

  2.1：172.18.21.71 设置网卡转发：
      ~]# vi /etc/sysctl.conf
          net.ipv4.ip_forward = 1
      ~]# sysctl -p
          net.ipv4.ip_forward = 1
      查看网卡妆发：
      ~]# cat /proc/sys/net/ipv4/ip_forward

  2.1：设置网络
      (1)：172.18.21.71 子网掩码：255.255.0.0 设置网关 设置DNS
      (2)：172.18.21.72 子网掩码：255.255.0.0 设置网关 设置DNS
      (3)：172.18.21.73  子网掩码：255.255.0.0 设置网关 设置DNS

  2.3：安装nginx设置网页
      在 172.18.21.72 和 172.18.21.73 安装 nginx
      ~]# yum -y install nginx
      ~]# systemctl start nginx.service

      设置首页以便查看效果：
       ~]# vi /usr/nginx/share/html/index.html
        192.168.1.11 :
            <h1>RS1</h1>
        192.168.1.12 :
            <h1>RS2</h1>


  2.5：防止通告与被通告
      172.18.21.72 与 172.18.21.73 执行：
      ~]# echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
      ~]# echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
      ~]# echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
      ~]# echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
      ~]# ifconfig lo:0 172.18.21.71 netmask 255.255.255.255 broadcast 172.18.21.71
      ~]# route add -host 172.18.21.71 dev lo:0


  2.6：设置ipvs
    ~]# ipvsadm -C
    ~]# ipvsadm -A -t 172.18.21.71:80 -s rr
    ~]# ipvsadm -a -t 172.18.21.71:80 -r 172.18.21.72:80 -g -w 1
    ~]# ipvsadm -a -t 172.18.21.71:80 -r 172.18.21.73:80 -g -w 2
    ~]# ipvsadm -Ln
      IP Virtual Server version 1.2.1 (size=4096)
      Prot LocalAddress:Port Scheduler Flags
        -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
      TCP  172.18.21.71:80 rr
        -> 172.18.21.72:80              Route   1      0          0         
        -> 172.18.21.73:80              Route   2      0          0         







[root@localhost ~]# curl 172.18.21.71
<h1>RS2<h1>
[root@localhost ~]# curl 172.18.21.71
<h1>RS1<h1>
[root@localhost ~]# curl 172.18.21.71
<h1>RS2<h1>
[root@localhost ~]# curl 172.18.21.71
<h1>RS1<h1>
[root@localhost ~]# curl 172.18.21.71
<h1>RS2<h1>
[root@localhost ~]# curl 172.18.21.71
<h1>RS1<h1>
[root@localhost ~]# curl 172.18.21.71
<h1>RS2<h1>
[root@localhost ~]# curl 172.18.21.71
<h1>RS1<h1>
[root@localhost ~]# curl 172.18.21.71
<h1>RS2<h1>
[root@localhost ~]# curl 172.18.21.71
<h1>RS1<h1>




~]# ipvsadm -E -t 172.18.21.71:80 -s sh





~]# ping 172.18.21.71

ping 172.18.21.71
~]# arp -vn | grep 172.18.21.71
172.18.21.71             ether   00:0c:29:fb:47:47   C                     eth0


  2.4：172.18.21.71 设置网卡
      ~]# ifconfig eno16777736:0 172.18.21.71 netmask 255.255.255.255 broadcast 172.18.21.71





[root@localhost keepalived]# curl 172.18.21.71
^C
[root@localhost keepalived]# ping 172.18.21.71
PING 172.18.21.71 (172.18.21.71) 56(84) bytes of data.
64 bytes from 172.18.21.71: icmp_seq=1 ttl=64 time=0.398 ms
64 bytes from 172.18.21.71: icmp_seq=2 ttl=64 time=0.227 ms
64 bytes from 172.18.21.71: icmp_seq=3 ttl=64 time=0.386 ms
64 bytes from 172.18.21.71: icmp_seq=4 ttl=64 time=0.574 ms
64 bytes from 172.18.21.71: icmp_seq=5 ttl=64 time=0.316 ms
64 bytes from 172.18.21.71: icmp_seq=6 ttl=64 time=0.266 ms
64 bytes from 172.18.21.71: icmp_seq=7 ttl=64 time=0.185 ms
64 bytes from 172.18.21.71: icmp_seq=8 ttl=64 time=0.482 ms
64 bytes from 172.18.21.71: icmp_seq=9 ttl=64 time=0.689 ms
^C
--- 172.18.21.71 ping statistics ---
9 packets transmitted, 9 received, 0% packet loss, time 8002ms
rtt min/avg/max/mdev = 0.185/0.391/0.689/0.157 ms
[root@localhost keepalived]# arp -vn | grep 172.18.21.71
172.18.21.71             ether   00:0c:29:4c:2a:d5   C                     eno16777736
